$(function() {

    // Initialize the jQuery File Upload widget:
    //$('#fileupload').fileupload();

    // Enable iframe cross-domain access via redirect option:



    function checkLoginFields() {
        var flag = true;

        $("#register-form :text").each(function() {
            if ($(this).val().length < 2) {
                flag = false;
            }
        });

        if ($("#register-form input:checkbox").is(':checked') && flag) {
            document.getElementById("register-button").disabled = false;
            return true;
        } else {
            document.getElementById("register-button").disabled = true;
            return false;
        }
    }

    $("#register-form :text").focusout(function() {
        checkLoginFields();
    });

    $("#register-form input:checkbox").click(function() {
        checkLoginFields();
    });

    $("#first-step-submit").submit(function(event) {
        if (checkLoginFields()) {
            event.preventDefault();
            var $form = $(this), name = $form.find("#nameUniverse").val(), desc = $form.find("#descUniverse").val();

            var posting = $.post("/firstStep", {
                n: name,
                d: desc
            }).done(function(data) {
                alert("Tutto OK! " + data);
            });
        }
    });

    $(".password-recover a").click(function(e) {
        e.preventDefault();
        $("#passwordRecoveryModal").modal('show');
        $("#passwordRecoveryModal .response-container").hide();
        $("#passwordRecoveryModal .insert-container").show();
        $("#close-recovery").hide();
        $("#delete-cancel").show();
        $("#confirm_recovery").show();
    });

    $("#confirm_recovery").click(function() {
        var insertedEmail = $("#email-recover").val();
        if (isValidEmailAddress(insertedEmail)) {
            $("#email_notification").hide();
            $.get("/passwordRecoverServlet", {
                email: insertedEmail
            }).done(function(data) {
                $("#passwordRecoveryModal .response-container").html(data);
                $("#passwordRecoveryModal .response-container").show();
                $("#passwordRecoveryModal .insert-container").hide();
                $("#close-recovery").show();
                $("#delete-cancel").hide();
                $("#confirm_recovery").hide();
            });
        } else {
            $("#email_notification").html("Inserisci un indirizzo email valido.");
            $("#email_notification").show();
        }
    });

    if ($("#recovery-result-container").length) {
        
        $("#newPasswordModal").modal('show');

        $("#newPasswordModal .response-container").hide();
        $("#newPasswordModal .insert-container").show();
        $("#close-new-psw").hide();
        $("#cancel-new-psw").show();
        $("#confirm-new-psw").show();

    }

    $("#confirm-new-psw").click(function() {
        var newPass = $("#new_email").val();
        var bundle = $("#bundle").val();
        switch (checkNewPassFields()) {
            case 0:
                $("#new_email_confirm_notification").hide();
                $.post("/passwordRecoverServlet", {
                    code: bundle,
                    password: newPass
                }).done(function(data) {
                    $("#newPasswordModal .response-container").html(data);
                    $("#newPasswordModal .response-container").show();
                    $("#newPasswordModal .insert-container").hide();
                    $("#close-new-psw").show();
                    $("#cancel-new-psw").hide();
                    $("#confirm-new-psw").hide();
                });
                break;
            case 1:
                $("#new_email_confirm_notification").html("Devi inserire in entrambi i campi la nuova password!");
                $("#new_email_confirm_notification").show();
                break;
            case 2:
                $("#new_email_confirm_notification").html("Devi inserire la stessa password in entrambi i campi, prova a reinserirla di nuovo.");
                $("#new_email_confirm_notification").show();
                break;
        }


    });

    function checkNewPassFields() {
        var newPass = $("#new_email").val();
        var newPassConfirm = $("#new_email_confirm").val();

        if (newPass === "" || newPassConfirm === "")
            return 1;
        else if (newPass !== newPassConfirm)
            return 2;
        return 0;
    }

    function isValidEmailAddress(emailAddress) {
        //        ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
    }


}); 